package readwrite;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import simulator.House;

public class Reader 
{
    
    private String version;
    private LinkedList wordsList = new LinkedList();
    
    private String fileName = "Building1.txt";
    //Return variables
    private String houseArea;
    private String population;
    private String wallsHeight;
    private String houseType;
    private ArrayList<String> tempIn = new ArrayList<String>();
    private ArrayList<String> tempOut = new ArrayList<String>();
    private ArrayList<String> illumination = new ArrayList<String>();
   
    private ArrayList<Double> temperaturInHouse = new ArrayList<Double>();
    private ArrayList<Double> temperaturOutHouse = new ArrayList<Double>();
    private ArrayList<Double> illuminationHouse = new ArrayList<Double>();
    
    private House house;
    
    public Reader(String _fileName) throws IOException
    {
    //Check file format      
        try
        {
            FileInputStream fStream = new FileInputStream(_fileName);
            DataInputStream dataInputStream = new DataInputStream(fStream);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream));
            String temp;
            
            temp = bufferedReader.readLine();
            if(temp.equals("POWER SIMULATOR FILE"))
            {
                temp = bufferedReader.readLine();
                version = temp.replaceAll("[A-Za-z ]", "");
                System.out.println("File version " + version);
                dataInputStream.close();
                
                //Check file content
                
                if(version.equals("1"))
                {
                
                    File file = new File(_fileName);
        
                    Scanner scan = new Scanner(new FileReader(file));
                                
                    while(scan.hasNext())
                    {
                        String next = scan.next();
                        wordsList.add(next);
                    }
        
                    //System.out.println(wordsList);
                    //System.out.println("House Area " + HouseArea(wordsList));
                    dataInputStream.close();
                }//end of if (version)
            }//end of if
            else
            {
                dataInputStream.close();
                System.out.println("Wrong file format");
            }//end of else
            
            /*
            while((temp = bufferedReader.readLine()) != null)
            {
                wordsList.add(temp);
            }//end of while
            */
            
            System.out.println(wordsList);
            
        }//end of try
        catch (FileNotFoundException ex) 
        {
            Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
        }//end of catch
    }
    
    private void HouseArea()
    {
        //houseArea = null;
        
        for(int i = 0; i < wordsList.size(); i++)
        {
            if(wordsList.get(i).equals("HouseArea"))
            {
                houseArea = wordsList.get(i + 1).toString();
                i = wordsList.size() + 1;
                
            }
            //System.out.println(i);
        }
        //return houseArea;
    }
    
    private void Population()
    {
        population = null;
        
        for(int i = 0; i < wordsList.size(); i++)
        {
            if(wordsList.get(i).equals("Population"))
            {
                population = wordsList.get(i + 1).toString();
                i = wordsList.size() + 1;
                
            }
        }
        
        //return population;
    }
    
    private void WallsHeight()
    {
        wallsHeight = null;
        
        for(int i = 0; i < wordsList.size(); i++)
        {
            if(wordsList.get(i).equals("WallsHeight"))
            {
                wallsHeight = wordsList.get(i + 1).toString();
                i = wordsList.size() + 1;
                
            }
        }
        
        //return wallsHeight;
    }
    
    private void HouseType()
    {
        houseType = null;
        
        for(int i = 0; i < wordsList.size(); i++)
        {
            if(wordsList.get(i).equals("HouseType"))
            {
                houseType = wordsList.get(i + 1).toString();
                i = wordsList.size() + 1;
                
            }
        }
        
        //return houseType;
    }
    
    private void TempIn()
    {
        for(int j = 0; j < 25; j++)
        {
            for(int i = 0; i < wordsList.size(); i++)
            {
                //System.out.println("TempIn" + j);
                if(wordsList.get(i).equals("TempIn" + j))
                {
                    tempIn.add(j, wordsList.get(i + 1).toString());
                    i = wordsList.size() + 1;    
                }
            }
        }
        
        //return tempIn;
    }
    
    private void TempOut()
    {
        for(int j = 0; j < 25; j++)
        {
            for(int i = 0; i < wordsList.size(); i++)
            {
                //System.out.println("TempIn" + j);
                if(wordsList.get(i).equals("TempOut" + j))
                {
                    tempOut.add(j, wordsList.get(i + 1).toString());
                    i = wordsList.size() + 1;    
                }
            }
        }
        
        //return tempOut;
    }
    
    private void Illumination()
    {
        for(int j = 0; j < 25; j++)
        {
            for(int i = 0; i < wordsList.size(); i++)
            {
                //System.out.println("TempIn" + j);
                if(wordsList.get(i).equals("Light" + j))
                {
                    illumination.add(j, wordsList.get(i + 1).toString());
                    i = wordsList.size() + 1;    
                }
            }
        }
        
        //return illumination;
    }
    
    public void Converter()
    {
        for(int i = 0; i < 25; i++)
        {
            temperaturInHouse.add(Double.parseDouble(tempIn.get(i)));
            temperaturOutHouse.add(Double.parseDouble(tempOut.get(i)));
            illuminationHouse.add(Double.parseDouble(illumination.get(i)));
        }
    }
    
    private void HousePreparation()
    {
        //Read all from file
        this.Population();
        this.HouseArea();
        this.TempIn();
        this.TempOut();
        this.Illumination();
        this.HouseType();
        this.WallsHeight();
        //Convert String arrays to Double arrays
        this.Converter();
    }
    
    public House FromFile()
    {
        this.HousePreparation();
        house = new House(Integer.parseInt(population), Integer.parseInt(houseArea), temperaturOutHouse, temperaturInHouse, illuminationHouse, Integer.parseInt(houseType), Integer.parseInt(wallsHeight));        
        return house;
    }
    
    
    
    public static void main(String args[]) throws IOException
    {
        Reader reader = new Reader("Building1.txt");
        reader.HousePreparation();
        reader.FromFile();
        System.out.println(reader.houseArea);
        
        /*
        System.out.println("HouseArea " + reader.HouseArea());
        System.out.println("Population " + reader.Population());
        System.out.println("WallsHeight " + reader.WallsHeight());
        System.out.println("HouseType " + reader.HouseType());
        
        //for(int i = 1; i < 25; i++)
            System.out.println("TempIn" + " " + reader.TempIn());
            System.out.println("TempOut" + " " + reader.TempOut());
            System.out.println("Light" + " " + reader.Illumination());
        */
    }//end of main
    
}//end of Reader
